

###########################################
## SLES Bill Stage Coding Terms
###########################################

##########
#### General Coding Protocol
##########
# ~~ AIC: Any action occurring within the committee, including having a hearing, markup, receiving a committee vote or committee report (positive or negative)
# ----- Note that in many cases, we use actions that imply prior action to code AIC
# ----- Example: Being reported out implies a positive vote in committee.
# ----- Example 2: Receiving a Do Not Pass recommendation/Failing to Pass in committee -- even if it kills the bill -- implies it was on the committee agenda
#
# ~~ ABC: Any action on the floor of the introducing chamber, including being reported out of committee, which in nearly all circumstances leads to a bill hitting the calendar
# ----- Note: Being reported out of 1st committee = ABC, even if then re-referred to a new committee. 
# ----- Note: In cases where a Do Not Pass Committee report effectively kills the bill (dies or requires supermajority to bring to floor), we leave ABC = 0. 
# ----- Often, however, DNP or Unfavorable reports still lead to it being moved to the floor
#
# ~~ Pass = Passed introducing chamber
# ----- When possible, we adjust for passage followed by a motion to reconsider and check if the bill actually made it to opposing chamber
#
# ~~ Law = The bill became law, with or without governor's signature
#
#########
##### Notes about what's included
##########
# 1. For each state, a vector of regular expressions that are matched against each state's legislative actions to code AIC, ABC, PASS, LAW
# ---> I've left the comments explaining specific terms, legislative processes, or issues that arose with using specific terms. 
#
# 2. Information about how we parsed the actions:
# ---> Typically, we restricted the coding of AIC, ABC, PASS to the introductory chamber only (i.e. dropped all out-chamber actions) 
# ---> In some cases, however, we ignored chamber information entirely, keeping all actions when coding 
# ---> This was usually needed in cases when the actions were scrambled, dates were not reliable, or the chambers processed bills simultaneously
# ---> Any state that has "ignore_chamber_switch = TRUE" below it falls in this category. 
#
# 3. Code for Pre- and Post-Processing the Action Text
# ---> Data for some states required significant pre-processing so that the actions could be easily passed to a generalized function (e.g., by 
# adding "comm:" to the beginning to clearly identify committee bills). In other cases, there wasn't enough information in the actions alone
# and we had to rely on post-coding checks to correctly code things that may have been missed. 
# ---> The direct implication of this is that the raw regex vectors may not work directly if used on your data absent similar pre-processing. 
# ---> To make it clearer where (and what) we've adjusted, I've included the relevant lines of code where we've fixed the action texts. 
# Pre-processing code is included before the regex vectors (usually on "bill_hist$actions", which represents a string table column in which each line represents a unique action)
# Post-processing is included after the regex vectors (usually on columns in a table called "bill_stages", such as "bill_stages$action_beyond_comm" or "bill_stages$law". 
# Adjustments to these columns represent direct updates to the codings that come out of our bill coding function). 

# ##############
# ### CONTEXT ON LANGUAGE IN THIS SCRIPT
# ##############
# ### The dataframe "bills" is referencing a file with 1 row per piece of qualifying legislation from that state/chamber
# ### The dataframe "bill_hist" is referencing a file with 1 row per action on each piece of qualifying legislation from that state/chamber
# ### The dataframe "bill_stages" is referencing the bill codes that come out of the our coding function (so it has codes for introduced/aic/abc/pass/law)
# 
# ##########################
# ##### Alaska
# ##########################
# 
# ### This is an example of how we edit the action text for Alaska bills (dropping chamber prefixes and appending a committee action flag)
# bill_hist$action <- str_trim(gsub('\\(H\\) |\\(S\\) ', '', bill_hist$action))
# bill_hist$action <- ifelse(bill_hist$action_location == "committeeAction", paste0('COMM: ', bill_hist$action), bill_hist$action)
# 
# ### That data then gets passed into a function that uses these terms to identify specific levels of action
# aic_t <- c("heard and held|heard \\& held|public hearing|comm:.+at.+ [am|pm]|^[^\\s]+ rpt |^dp:|^dnp:|^nr:|^am:")
# abc_t <- c("^[^\\s]+ rpt ",
#            "rules to calendar", "move.+out of comm", "read the second time", "read the third time", 
#            "advanced to third", "am no [0-9]+", "^passed", "^failed")
# pc_t <- c("^passed|transm[a-z]+ to \\(s\\)|transm[a-z]+ to \\(h\\)|transm[a-z]+ to gov")
# law_t <- c("signed into law|effective (date of|dateof|date\\(s\\) of|date\\(s\\)of) law|chapter [0-9]+ sla")
# 
# ### Unlike for some other states, we do not post-process the codings for Alaska as the regular expressions and 
# ### preprocessing are sufficient to capture all stages of bill progress
# 
# ##########################
# ###### Alabama
# ##########################
# 
# aic_t <- c('read.+ second time', '^reported from', '^acted on by', 'favorable from')
# abc_t <- c('read.+ second time', '^reported from', 'third reading', 'placed on the calendar', 'motion to')
# pc_t <- c('engrossed', 'enrolled', 'motion.+read a third time and pass.+adopted')
# law_t <- c('^assigned act no')
# 
# ##########################
# ####### Arkansas
# ##########################
# 
# aic_t <- c('returned by the committee', 'do pass', 'do not pass', 'without recommendation')
# abc_t <- c('returned by the committee', 'engrossed', 'third time', 'amendment.+read', 're-referred to',
#            'committee of the whole', 'placed on the calendar', 'placed on calendar')
# pc_t <- c('third time and passed', 'transmitted to the senate', 'transmitted to the house')
# law_t <- c('act [0-9]+')
# 
# if(bill_stages$law == 0 & !is.na(bills[i,]$act_num)){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# 
# ##########################
# ####### Arizona
# ##########################
# # ** Run note: ignore_chamber_switch = TRUE
# 
# bill_hist$action <- gsub('reported failed from ', 'COMMITTEE REPORT--FAILED~', tolower(bill_hist$action))
# bill_hist$action <- gsub('reported held from ', 'COMMITTEE REPORT--HELD~', tolower(bill_hist$action))
# bill_hist$action <- gsub('reported disc/held from ', 'COMMITTEE REPORT--DISC/HELD~', tolower(bill_hist$action))
# bill_hist$action <- gsub('reported w/d from ', 'COMMITTEE--WITHDRAWN~', tolower(bill_hist$action))
# 
# aic_t <- c("^reported.+from.+committee", 'committee report--')
# abc_t <- c('^reported.+from.+committee', 'third reading', 'committee of the whole', 'floor motion')
# pc_t <- c('transmitted to house', 'transmitted to senate', 'final reading', 'transmitted to gov', 'conference comm') # final = back to originating chamber 
# law_t <- c('^law', "chapter number")
# 
# ##########################
# ####### California
# ##########################
# 
# aic_t <- c("^in committee: (placed|motion|reconsider|referred|held|hearing for|further|refused)", "^from committee chair.+:",
#            "^from committee with author", "^from committee: (do pass|amend|be placed)",
#            "set.+ hearing. (referred|failed|further|held|testimony)")
# abc_t <- c("^from committee: (do pass|amend|be placed)", "^from committee chair.+:", "from committee with author",
#            "read second", "^re-referred")
# pc_t <- c("read third.+ passed|^enrolled|to enrollment|^vetoed", "passed\\. ordered to the (senate|assembly)")
# law_t <- c("^chaptered by|approved by the gover|chapter \\d+, statutes")
# 
# 
# ##########################
# ####### Colorado
# ##########################
# 
# #### 1999 or Earlier
# aic_t_99 <- c('_committee amends', '_committee refers', '_committe.+sends to floor', '_committe.+sends bill to floor', 
#               '_committee send to [a-z]_committee', '_committee sends bill to [a-z]_committee',
#               "_committee pi's this bill")
# # --> PI = Postpones indefinitely --> Implies a hearing -- see terms
# abc_t_99 <- c('_committe.+sends to floor', '_committe.+sends bill to floor', 
#               'h2nd', 'h3rd', 's2nd', 's3rd', '^bill is amended', '^bill is lost', '^bill is laid over')
# pc_t_99 <- c('bill passes h3rd', 'sent to senate', 'bill passes s3rd', 'senate to house')
# law_t_99 <- c('signed into law', 'bill becomes law')
# 
# #### 2000 to 2015+ ---> Format stays the same with new website in 2016
# aic_t <- c('committee on.+pass unamended', 'committee on.+amended', 'committee.+postpone indefin',
#            'committee.+lay over', 'committee on.+refer.+to', 'witness testimony', 'committee discussion',
#            'committee.+to (house|senate) committee of the whole')
# abc_t <- c('committee.+to (house|senate) committee of the whole', 'second reading', 'third reading')
# pc_t <- c('^house third reading passed', '^senate third reading passed')
# law_t <- c('governor signed', 'governor became law', 'governor partial veto', 
#            'governor action - signed', 'governor action - became law', 'governor action - partial veto')
# 
# ### 2015 Add Check for Passage -- Occasionally introduced/recorded in out-chamber before passage of intro chamber when happen on same date
# if(bill_stages$passed_chamber == 0 & any(grepl("third reading passed", tolower(hist_sub$action)))){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }
# ### 2015 Same as Above but for AIC
# chamb <- ifelse(grepl("^S", b_id), 'senate', 'house')
# if(bill_stages$action_in_comm == 0 & any(grepl(paste0(chamb, ' committee on.+'), tolower(hist_sub$action))) ){
#   bill_stages$action_in_comm <- 1
# }
# 
# 
# 
# ##########################
# ###### CONNECTICUT
# ##########################
# 
# aic_t <- c('public hearing', 'vote to draft', 'drafted by committee', 'favorable', 'unfavorable', 'change of reference', 'merged into')
# ### *** Bills referred first to a joint committee... 
# ### *** Favorable change of reference = Comm 1 refers to Comm 2 with favorable recommendation - https://www.cga.ct.gov/asp/content/terms.asp
# abc_t <- c('favorable report', 'joint favorable$', 'joint favorable sub', 'house favorable', 'senate favorable', 'house calendar', 'senate calendar',
#            'referred by house', 'referred by senate', 'house adopted.+amend', 'senate adopted.+amend')
# # *** After committee, bills go to legislative commissioner's office to be checked for constitutionality/conflicts with other law
# # --> BUT -- in records, often goes there before reported out, so 'filed with legislative commissioner', 'reported out of legislative commissioner' != ABC
# pc_t <- c('^house passed', '^senate passed', 'in concurrence', 'secretary of state', '^public act', '^special act')
# law_t <- c('signed by governor', 'signed by the governor', 'public act', '^became law') 
# 
# 
# 
# ##########################
# ###### Delaware
# ##########################
# 
# aic_t <- c('reported out', '^tabled in') ## The only committee actions are introduced, assigned to, or reported out
# abc_t <- c('reported out', 'amendment.+in (house|senate)', 'amendent (ha|sa).+passed', 'amendent (ha|sa).+defeated',
#            'rules.+suspended.+(house|senate)', 'lifted from table')
# pc_t <- c('^passed', 'vetoed', "passed by (house of representatives|senate)")
# law_t <- c('^signed by gov', '^enact')
# 
# if(bill_stages$law == 0 & tolower(bills[i,]$status) %in% c("enact w/o sign", "signed")){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# if(bill_stages$passed_chamber == 0 & tolower(bills[i,]$status) %in% c("passed")){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }
# if(bill_stages$action_beyond_comm == 0 & tolower(bills[i,]$status) %in% c("out of committee")){
#   bill_stages$action_beyond_comm <- 1
# }
# 
# 
# ##########################
# ####### Florida
# ##########################
# 
# aic_t <- c('on committee agenda', 'on subcommittee agenda', 'on council agenda', '^favorable|; favorable', 
#            '^recommendation', '^unfavorable|; unfavorable', '^cs by|; cs by', '^cs[a-z\\/]+ by') 
# abc_t <- c('^favorable|; favorable', '^cs by|; cs by', 'read second time', 'amendment\\(s\\) adopted', 'amendment\\(s\\) failed', 'read third time',
#            'special order calendar', 'local calendar', 'placed on consent calendar', 'placed on calendar -(hj|sj)',
#            'read 2nd time', 'read 3rd time', 'on 3rd reading', 'on 2nd reading')
# pc_t <- c('read third time.+passed', 'read second and third time.+passed', '^passed', '^cs passed', 'ordered enrolled')
# law_t <- c('chapter no\\. [0-9]+', 'approved by governor', 'law without governor')
# 
# ### Dropping bills with "Died, not introduced"
# if(sum(bill_stages[6:9]) == 0 & grepl("died, not introduced", tolower(hist_sub[nrow(hist_sub),]$action)) ){
#   bill_stages$introduced <- 0
# }
# 
# 
# ##########################
# ###### Georgia
# ##########################
# 
# aic_t <- c('house committee', 'senate committee')
# # ** NOTE: In S, 2nd read indicative of floor action; in H, 2nd read occurs while bill still in committee
# abc_t <- c('committee favorably', 'senate read second', 'house third', 'senate third', 'engrossed prevailed', 'tabled',
#            'taken from table', 'senate recommitted', 'house recommitted')
# # --> not including 'house notice of motion to engross' as that happens at introduction 
# pc_t <- c('house passed', 'senate passed', 'sent to gov', 'transmit.+senate', 'transmit.+house')
# law_t <- c('^act [0-9]+', 'signed by gov', '^effective date')
# 
# 
# ##########################
# ###### Hawaii
# ##########################
# 
# aic_t <- c('scheduled to be heard by', 'the committ.+ recommend', 'scheduled for decision making on', 'the votes in [a-z]+ were', 'conference room [0-9]',
#            'the committ.+has sched', 'the committ.+ deferred')
# # ** 'conference room [0-9]' could also pick up conference committees but subsetting to intro-chamber only will prevent this
# abc_t <- c('^reported from', '^report adopted', 'passed second reading', 'failed to pas second', 'passed third reading', 'failed to pass third',
#            '48.+notice', 'forty-eight.+notice', 'placed on the cal.+ for', 'floor amendment',  '^laid on the table',
#            '^re-referral to the committ', 'recalled from the commit', 'recommitted to the commit')
# ## ** 48 hours/hrs. notice
# pc_t <- c('passed third reading', 'transmitted to house', 'transmitted to senate', 'enrolled')
# law_t <- c('^act [0-9]+')
# 
# 
# ##########################
# #### IOWA:
# ##########################
# 
# aic_t <- c('subcommittee', '^committee report', '^recommended', 'committee amendment', 
#            'placed on (ways and means|appropriations) calendar')
# # --> NOBA != AIC: "NOBAs (Notes on Bills and Amendments) are prepared by the Fiscal Services Division of LSA"
# abc_t <- c('^placed on calendar', '^committee report', 'amendment.+(adopted|lost)', 'point of order', 'out of order')
# pc_t <- c('^passed house', '^passed senate', 'enrolled', '^(house|senate) concurred')
# 
# law_t <- c('signed by governor') 
# 
# ### Code AIC = 1 if bill came from H/S Study Bill (depending on chamber) -- Indicative of Committee Action
# # -- Committee bills often introduced as study bills, then transition to real bills that go straight to floor. Issue is we then don't see AIC. 
# related_bills <- bills[i,]$related_bills 
# if(bill_stages$action_in_comm == 0 & grepl(glue('{substring(b_id,1,1)}SB|LSB'), related_bills) ){
#   bill_stages$action_in_comm <- 1
# }
# 
# 
# ##########################
# ##### Idaho
# ##########################
# 
# if(t <= 2008){
#   aic_t <- c('rpt out', 'rec d/p')
#   abc_t <- c('rpt out', '2nd rdg', '3rd rdg', 'to gen ord', 'engross')
#   pc_t <- c('3rd rdg - passed', 'rls susp - passed')
#   law_t <- c("governor signed", "session law")
# }else{
#   aic_t <- c("reported out", "do pass", "do not pass", "without recommendation")
#   abc_t <- c("reported out", "(second|2nd) reading", "(third|3rd) reading", "engross", "committee of the whole")
#   pc_t <- c("read (third|three).+passed")
#   law_t <- c("signed by governor", "session law", 'became law without governor')
# }
# 
# ## Cross-Checking to See if Enrolled/Introduced In Opposite Chamber (Passed language sometimes in middle of row)
# if(t < 2009){
#   if(any(grepl("to enrol|rpt enrol|sp signed|pres signed", hist_sub$action))){
#     bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
#   }else if(substring(b_id, 1, 1) == "H" & any(grepl('senate intro', hist_sub$action))){
#     bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
#   }else if(substring(b_id, 1, 1) == "S" & any(grepl('house intro', hist_sub$action))){
#     bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
#   }
# }else{
#   if(any(grepl("signed by (the speaker|speaker)|signed by (the president|president)|(house|senate) concurred", hist_sub$action))){
#     bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
#   }else if(substring(b_id, 1, 1) == "H" & any(grepl('received from (the house|house)', hist_sub$action))){
#     bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
#   }else if(substring(b_id, 1, 1) == "S" & any(grepl('received from (the senate|senate)', hist_sub$action))){
#     bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
#   }
# }
# 
# 
# ##########################
# #### ILLINOIS
# ##########################
# 
# aic_t <- c('^do pass', 'senate comm[a-z]+ amend', 'house comm[a-z]+ amend', '^amendment no.+ h$', '^amendment no.+ s$', 
#            'comm[a-z]+ deadline extend', 'motion filed.+comm', 
#            'do pass.+short debate', 'do pass.+standard debate', 'motion do pass',
#            'rec.+subcomm', 'to.+subcomm')
# abc_t <- c('^do pass', '2nd read', '3rd read', 'second read', 'third read', 'house floor amend',
#            'senate floor amend', '^amendment no.+[a-z][a-z]$')
# pc_t <- c('passed$', 'passed [0-9]', 'passed ~', '/passed', '/pass ~', '^passed both', '^third reading - passed') 
# law_t <- c('^public act.+[0-9]', 'governor approved') 
# 
# 
# ##########################
# ##### Indiana
# ##########################
# 
# # Recoding Failed Committee Action
# if( any(grepl('Committee report: rejected', bill_hist$action)) ){
#   bill_hist[bill_hist$action == "Committee report: rejected",]$action <- 'aic~committee report: rejected' 
# }
# 
# aic_t <- c('^committee report', "^aic~committee report:")
# abc_t <-c("^committee report", "second reading", "third reading", "rules suspended", 'engrossed')
# pc_t <- c('third reading: passed', 'referred to the senate', 'referred to the house', 'conference comm')
# law_t <- c('^public law', 'signed by the gov')
# 
# 
# ##########################
# #### Kentucky
# ##########################
# 
# # Adjust "Passed Over" so as not to accidentally code as Passed Chamber == 1
# bill_hist$action <- gsub("^passed over", 'bill passed over', bill_hist$action)
# 
# aic_t <- c('^reported', 'committee substitute', 'committee amend', 'posted in committee')
# abc_t <- c('^reported', 'to calendar', 'to consent calendar', '1st reading', '2nd reading', '3rd reading',
#            'posted for passage', 'floor amendment', '^defeated', '^passed', 'recommitted to')
# # 1st reading occurs after bill reported... sent to rules after 2nd, where it can then be recommmitted
# pc_t <- c('^passed', '^3rd reading, passed')
# law_t <- c('^signed by gov', '\\(acts ch\\. [0-9]+\\)', 'secretary of state.+ ch\\. [0-9]+', 'became law without gov')
# 
# if(bill_stages$passed_chamber == 0 & any(grepl('enrolled', hist_sub$action)) ){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }
# 
# ##########################
# #### Louisiana
# ##########################
# 
# aic_t <- c('^reported', 'scheduled to be heard', 'considered but not reported', 'reported favorably', 
#            'reported with [a-z]', 'reported by', 'reported without a[a-z]+')
# abc_t <-c('^reported', 'reported without action', 'special order', 'read third time', 'engrossed', 
#           'third reading', 'regular calendar', 'called from the calendar', 
#           'floor amendments')
# # Comm can report Unfavorably, but still gets read to the Chamber (see Senate Rule 10.12) --> Any bill that gets reported --> ABC
# pc_t <- c('finally passed', 'passed.+sent to the (house|senate)', 'ordered to the (house|senate)',
#           'received in the (house|senate)', 'enrolled')
# law_t <- c('signed by the governor', 'act no. [0-9]+', 'act # [0-9]+', '^effective date')
# 
# 
# ##########################
# #### Massachusetts
# ##########################
# # ** Note: ignore_chamber_switch = TRUE, add_chamb = "Joint"
# 
# aic_t <- c('reported favorab', '^recommend', 'committee recomm', 'ought not to pass', 'ought to pass', 
#            '^committee reported that', 'public hearing', 'hearing date', 'hearing [a-z]+heduled',
#            'discharged to.+comm')
# # discharged to = committee decides bill is out of its jurisdiction and moves it elsewhere (https://malegislature.gov/StateHouse/Glossary#D)
# abc_t <- c('reported [a-z]+', 'orders of the day', '^amendment', 'motion to suspend', 
#            'read second', 'read third', 'third read')
# pc_t <- c('passed.+engrossed', 'enacted') 
# law_t <- c('signed by the gov', 'chapter [0-9]+ +of', 'became law pursant to')
# 
# 
# ##########################
# #### Maryland
# ##########################
# # ** Note:  ignore_chamber_switch = TRUE
# 
# aic_t <- c('hearing', 'report by', 'favorable', 'unfavorable', 'no recommendation', 'committee amendment',
#            'referred.+interim study') # interim study kills bill, but implies action
# abc_t <- c('favorable.+report', 'report adopted', 'floor.+amendment', 'second reading', 'third reading', 'calendar')
# ## Report adopted/not by whole chamber (precursor to second reading)
# pc_t <- c('^third reading passed', 'enrolled') 
# law_t <- c('signed by the gov', 'chapter [0-9]+')
# 
# ##########################
# #### Maine
# ##########################
# # **  Note: add_chamb = "Joint Committee", ignore_chamber_switch = TRUE
# 
# aic_t <- c('reported out', 'otp', 'ontp', 'public hearing', 'work session', '^voted$',
#            '^voted - [a-z\\-]+$', 'committee amendment', 'divided report', 'div. rep.')
# # Rule 310.3 == Ought Not to Pass --- http://openmepolitics.com/glossary/joint-rule-310-3/
# # --> these get reported out, but are basically dead on arrival
# # --> recoding in loop as ABC = 0; will get coded as ABC = 1 in rare cases where bill is revived from dead
# abc_t <- c('reported out', 'accepted report', 'reports read', 'engrossed', 'enacted', 'reconsidered', 'removed from table',
#            'amendment adopted', 'first reading', 'second reading',
#            'report.+accepted', 'consent calendar', 'read a second time')
# pc_t <- c('^engrossed.+prevail', '(insisted|adhered).+engrossed.+prevails', 'enacted.+prevail', 'finally passed.+prevails',
#           'passed to be engross', 'passed to be enacted')
# ## -- After engrossment, it goes to the other chamber... http://legislature.maine.gov/general/path-of-legislation-in-maine-detailed/9285
# ## BUT... still has to then pass both chambers for enactment... even if both pass the same version. So both have to revote.
# ## So: Coding engrossment as passed chamber and keeping enactment/finally passed as double check
# ## Related: "(insisted|adhered) to engrossed" occurs if out-chamber disagrees, in-chamber can reconsider and adhere or not adhere to initial engrossment (http://legislature.maine.gov/general/path-of-legislation-in-maine-detailed/9285)
# law_t <- NA # Coded directly via data on chapter numbers
# 
# # Check if Law
# if( grepl('chapter [0-9]+', tolower(bills[i,]$chapter_num))){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# # Check if Passed 
# # -- Removing: "died between houses" as in some cases this can indicate passed OPPOSING chamber after intro chamber accepted 'ontp' report
# if( grepl('engross|enacted|finally passed|veto', tolower(bills[i,]$status))){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }
# # Check if ABC
# if(grepl('accepted.+report|enactment failed', tolower(bills[i,]$status))){
#   bill_stages$action_in_comm <- bill_stages$action_beyond_comm <- 1
# }
# # Check if Miscoded as ABC when Ought Not to Pass
# #-- NOT including "placed in legislative files \\(dead\\)" as this shows up sometimes when a bill dies other ways
# if(nrow(hist_sub) > 0){
#   if(bill_stages$action_beyond_comm == 1 & bill_stages$passed_chamber == 0 & grepl("joint rule 310|ontp", hist_sub[nrow(hist_sub),]$action)){
#     bill_stages$action_beyond_comm <- 0
#   }
# }
# 
# ##########################
# ##### Michigan
# ##########################
# 
# aic_t <- c('^reported') 
# abc_t <- c('^reported|(referred to|placed.+) second read|placed.+ third read|placed on immediate passage|^roll call|^amend|^substitute')
# pc_t <- c('^passed|enrolled')
# law_t <- c('approved by.+governor|assigned pa +[0-9]+')
# 
# 
# ##########################
# ###### Minnessota
# ##########################
# # ** Note: ignore_chamber_switch = TRUE
# 
# aic_t <- c('^comm rpt', '^committee report', '^committee rpt', '^comm report', 'referred by chair')
# abc_t <- c('^comm rpt', '^committee report', '^committee rpt', '^comm report', 'second reading', 
#            'third reading', 'taken from table', 'rules suspended','^re-referred', 'special order', '^amend', 
#            'point of order', '^general orders', 'placed on calendar')
# # 'designated special order' --> bill given floor priority for debate + other special orders = floor action
# pc_t <- c('third reading, passed', "third reading passed", '^bill was passed', 'received from house', 
#           'received from senate', '^bill was repassed')
# law_t <- c('signed by gov', "governor.+approval", "effective date") 
# 
# 
# 
# ##########################
# #### Missouri
# ##########################
# ## House
# H_aic_t <- c('hearing room', 'committee room', '^date:.+time:', 'reported do pass', "reported do not pass", 
#              'voted do pass', 'voted do not pass', 'public hearing', 'executive session')
# H_abc_t <- c('reported do pass', 'third read', '^perfected', 'ayes:', 'noes:', 'placed on.+calendar', 'taken up for')
# # Perfected = similar to engrossment in many chambers
# H_pc_t <- c('third read and passed', 'reported to the senate', '^signed by', 'truly agreed to and finally passed') 
# # 'delivered to secretary of state' != LAW ALWAYS; see, e.g, https://house.mo.gov/billtracking/bills01/action01/aHB909.htm
# H_law_t <- c('approved by governor', 'approved by the governor', 'no action taken by governor', 'vetoed in part by governor',
#              'passed over veto', 'approved.+acting governor')
# 
# ## Senate
# S_aic_t <- c('hearing conducted', 'hearing scheduled', 'voted do pass', 'voted do not pass', 'reported do', 'reported from')
# S_abc_t <- c('reported from', 'reported do pass', 'third read', 'perfected', 'calendar', 'taken up',
#              'bill taken from comm')
# S_pc_t <- c('third read and passed', 'truly agreed to and finally passed', 'duly enrolled', 'signed by')
# S_law_t <- c('signed by governor', 'signed by the governor', 'no action taken by governor', 'vetoed in part by governor',
#              'legislature voted to override', 'signed by acting governor')
# # ---> Senate doesn't record delivery to secretary of state -- except for some resolutions
# 
# ### Check Veto Overrides (Not always caught by terms in h_law/s_law)
# if(bill_stages$law == 0 & any(grepl("house votes to override veto|h adopt.+ override|motion to override.+ h adopted", tolower(hist_sub$action))) & any(grepl("senate votes to override veto|s adopt.+ override|motion to override.+ s adopted", tolower(hist_sub$action)))){
#   bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# 
# 
# ##########################
# ###### Mississippi
# ##########################
# 
# aic_t <- c('title suff', 'do pass', 'committee substitute', 'tsdp', 'do be reref')
# ### TSDP seems to be if assigned to two committees, transmitting rec across bodies (so seemingly act as 1)
# abc_t <- c('title suff', 'do pass', 'point of order', '^pass', '^fail', '^amend', 'defeat', 'tabled', 'on calendar',
#            'committee substitute adopted', 'reconsider', 'motion to', 'read the third time', 'recommitted')
# pc_t <- c('^passed', 'transmitted to senate', 'transmitted to house', 'enrolled bill signed')
# law_t <- c('approved by gov', 'law w.out governor', 'partially vetoed by gov')
# 
# if(bill_stages$law == 0 & bills[i,]$status == "Law"){
#   bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# 
# ##########################
# ###### Montana
# ##########################
# # Note ** ignore_chamber_switch = TRUE
# 
# ### Remove Chamber at beginning of Action
# bill_hist$action <- gsub('^\\([A-Z]\\) ', '', bill_hist$action)
# 
# aic_t <- c('^hearing ~', '^committee executive action', '^committee report', 'tabled in committee')
# abc_t <- c("2nd reading", '3rd reading', 'second reading', 'third reading')   #, 'rereferred to committee' -- not always abc, sometimes after first reading
# pc_t <- c('3rd reading passed', 'third reading passed', 'transmitted to senate', 'transmitted to house', 'enrolling', 'signed by speaker')
# law_t <- c('signed by governor', 'chapter number assigned')
# 
# ### Correcting AIC to 0 if sponsor requested bill not be heard (typically then tabled)
# if(bill_stages$action_in_comm == 1 & sum(bill_stages[,5:9]) == 2 & any(grepl("bill not heard at sponsor", tolower(hist_sub$action)))){
#   bill_stages$action_in_comm <- 0
# }
# 
# 
# ##########################
# #### North Carolina
# ##########################
# 
# aic_t <- c("^reptd", 'assigned to.+subcomm')
# abc_t <- c("^reptd fav", '^reptd without', '^reptd com sub', '^reptd as amend',
#            "amend adopted", "amend failed", 'amend pending', 'amends ruled material', 'amend recon',
#            "added to calendar", "placed on cal for", "passed 2nd", "failed 2nd")
# # Not using "engrossed" as occasionally happens BEFORE bill passes chamber of introduction
# pc_t <- c("passed 3rd reading", "passed 2nd \\& 3rd reading", "^adopted$", "message sent to", '^rec from house', '^rec from senate')
# 
# if(t_yrs == "1993_1994"){ 
#   law_t <- c("signed by gov", '^ratified', 'ratified ch.[0-9]+')
# }else{
#   law_t <- c("signed by gov", "ch\\. sl [0-9]+", '^ratified', 'ch.[0-9]+')
# }
# 
# if(bill_stages$passed_chamber == 0 & any(grepl("engrossed|^rec from", tolower(hist_sub$action))) & length(unique(hist_sub$chamber)) > 1){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }
# 
# ##########################
# #### North Dakota
# ##########################
# 
# aic_t <- c('committee hearing', 'reported back', 'do pass', 'do not pass', 'divided committee report', 'majority report')
# abc_t <- c('reported back', 'placed on calendar', 'second reading', '^amendment', 'passed', 
#            'failed', 'reconsidered', 'rereferred')
# pc_t <- c('second reading, passed')
# law_t <- c('signed by gov', 'filed with secretary of state')
# 
# 
# ##########################
# ### Nebraska
# ##########################
# 
# aic_t <- c('notice of hearing', 'public hearing', 'com am[0-9]+')
# # ---> No record of reporting out; seems like bills can be put on general file without committee action (in rare cases) (and also then postponed indefinitely from there)
# # ---> see, eg, https://nebraskalegislature.gov/bills/view_bill.php?DocumentID=4395
# abc_t <- c('placed on general file', 'placed on select file', 'placed on final', 'advanced to', 'engross', 'enroll',
#            '^passed', 'adopted$', 'lost$', 'pending$')
# pc_t <- c('^passed on', '^passed by', '^passed notwith', 'president.+signed', 'speaker.+signed')
# law_t <- c('approved by gov', 'passed notwithstanding', 'certificate') 
# # --> passed notwithstanding signifies veto override
# 
# 
# ##########################
# ##### New Hampshire
# ##########################
# 
# # NH Abbreviations: http://www.gencourt.state.nh.us/bill_status/docket_abbrev.htm
# aic_t <- c('^hearing', '^joint hearing', 'maj rep', 'min rep', 'committee report', "comm am", 
#            "prop [a-z]+ am .+ hc [0-9]", 'otp', 'itl', 'report rnf', 'report re-ref', "subcom",
#            "ought to pass", "inexpedient to legislate")
# # --> otp = ought to pass; itl = inexpedient to legislate; rnf = recommended but not funded
# abc_t <- c('committee report', 'maj rep', 'otp', 'report re-ref', ' aa ', '^aa ', 'am vv', 'fl am', 'floor am')
# #AA = amendment adopted
# pc_t <- c('passed', 'enrolled', 'ot3rdg')
# # --> senate, in 2000, starts using 'ot3rdg' for passed on third reading --- cross-checking that it actually swapped chambers should make sure this is right
# law_t <- c('signed by gov', 'chap\\.[0-9]+', 'signed by the gov', 'chapter [0-9]+', "chap: [0-9]+")
# 
# # Adjusting for bills coded "PASSED" but didn't actually make it out
# if(bill_stages$passed_chamber == 1 & length(unique(hist_sub$chamber)) == 1 & bill_stages$law != 1){
#   bill_stages$passed_chamber <- 0
# }
# # Adjusting for bills with Chapter Numbers that are Missing Actions
# if(bill_stages$law == 0 & !is.na(bills[i,]$chapter_num) ){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# # Adjusting for Bills that have Floor Dates but Coded ABC == 0
# if(bill_stages$action_beyond_comm == 0 & ((grepl("^S", bills[i,]$bill_id) & !is.na(bills[i,]$S_floor_date)) | (grepl("^H", bills[i,]$bill_id) & !is.na(bills[i,]$H_floor_date))) ){
#   bill_stages$action_beyond_comm <- 1
# }
# 
# 
# ##########################
# ######## New Jersey
# ##########################
# 
# aic_t <- c('reported out of', 'reported from', 'reported and referred', "public hearing") # 'transferred to' = Transferred to different comm (not clear who is responsible)
# # ----> No records of action besides lack of referral and sparse public hearings
# abc_t <- c('reported out of', 'reported from', 'reported and referred', '2nd reading', 'motion', 'assembly floor amendment', 'senate amendment', 'recommitted to')
# pc_t <- c('^passed assembly', '^passed senate', 'received in the assembly', 'received in the senate')
# law_t <- c('^approved', '^cvor$')
# # cvor = 'conditional veto override'
# 
# 
# ##########################
# ##### New Mexico
# ##########################
# 
# # *** NM: Actions are sporadically out of order -- no dates provided (besides "legislative days", which aren't always provided)
# aic_t <- c('reported by', 'do pass', 'do not pass', 'committee substitution', 'without recommendation')
# abc_t <- c('reported by committee with', 'placed.+calendar', 'floor amendment', '^failed to pass', 'by motion', 'motion to',
#            'withdrawn from comm.+placed on') 
# pc_t <- c('^passed', 'referrals: cc', 'failed to concur', 'has concurred')
# law_t <- c('signed by gov', 'chapter [0-9]+')
# 
# ## Check Law Codings
# if(bill_stages$law == 0 & bills[i,]$status == "Chaptered"){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# 
# # Check Pass Codings --- In 2014, bunch of bills that passed chamber but not recorded in actions... can check using reports (e.g., if have a house and senate report)
# if(bill_stages$passed_chamber == 0 & bills[i,]$status %in% c("Passed", "Vetoed", "Pocket Veto") ){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }
# if(t_yrs == "2013_2014"){
#   if(bill_stages$passed_chamber == 0 & grepl("\\bH[A-Z]+ Committee Report", bills[i,]$comm_reports) & grepl("\\bS[A-Z]+ Committee Report", bills[i,]$comm_reports)){
#     bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
#   }
# }
# 
# ## Check AIC Coding -- Need to avoid things like "fiscal impact report" 
# if(bill_stages$action_in_comm == 0 & grepl("[A-Z][A-Z]+ (Committee Report|CR|CS)|Committee (Vote|Substitute)",  bills[i,]$comm_reports) ){
#   bill_stages$action_in_comm <- 1
# }
# 
# ### Check ABC Coding: Need to DROP reports by Senate Committee's Committee and House Rules and Order of Business Committee (Germane Rulings)
# check_reports <- gsub("(SCC|HRC) CR|(SCC|HRC) Committee Report", "", bills[i,]$comm_reports)
# if(bill_stages$action_beyond_comm == 0 & grepl("[A-Z][A-Z]+ (Committee Report|CR)", check_reports) ){
#   bill_stages$action_in_comm <- 1
# }
# 
# ## Fix Bills with No Action Information (Much of these will be fixed by the above corrections)
# if(s_id == '2001-SS2' & b_id == "HB0001"){
#   bill_stages$action_in_comm <- bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }else if(s_id == '2007-SS1'){
#   # See: https://www.nmlegis.gov/Legislation/Legislation_List --> No action on any of the senate bills...
#   if(b_id %in% c("HB0001", "HB0002", "HB0003", "HB0004", "HB0005", "HB0006", "HB0008")){
#     bill_stages$action_in_comm <- bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
#   }else if(b_id %in% c("HB0007")){
#     bill_stages$action_in_comm <- bill_stages$action_beyond_comm <- 1
#   } 
# }else if(s_id == '2014-RS'){
#   # HB 11 + HB302 - HB406 + SB269 - SB379
#   # No AIC: HB 302-3; 307, 312-314; 317, 319-324; 326, 329, 331-2, 336, 339-41, 343-45, 347, 349-53, 357-8, 360-62, 364-72, 374-90, 392-406
#   # No AIC: SB 270-74; 278, 280, 284-286, 289, 294-298, 300-303, 308-309, 311, 314, 317-18, 321, 324-26, 328-29, 332-346, 348-367, 369-376, 378
#   reported <- c("HB0011", 'HB0304', "HB0306", "HB0308", "HB0309", "HB0310", 'HB0315', 'HB0316', 'HB0318', 'HB0325', 'HB0327', 
#                 'HB0334', 'HB0342', 'HB0346', 'HB0354', 'HB0355', 'HB0356', 'HB0359', 'HB0373', 'HB0391',
#                 'SB0269', 'SB0276', 'SB0277', 'SB0279', 'SB0281', 'SB0282', 'SB0283', 'SB0287', 'SB0288', 'SB0290', 'SB0291',
#                 'SB0292', 'SB0293', 'SB0299', 'SB0305', 'SB0306', 'SB0310', 'SB0315', 'SB0316', 'SB0319', 'SB0322', 'SB0323',
#                 'SB0327', 'SB0330', 'SB0331', 'SB0368', "SB0377", 'SB0379')
#   if(b_id %in% reported){
#     bill_stages$action_in_comm <- bill_stages$action_beyond_comm <- 1
#   }else if(b_id %in% c("HB0305", 'HB0311', 'HB0333', 'HB0335', 'HB0337', 'HB0348', 'HB0363', 
#                        'SB0275', 'SB0304', 'SB0312', 'SB0320')){
#     bill_stages$action_in_comm <- bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
#   }else if(b_id %in% c("HB0328", 'HB0330', 'HB0338', 'SB0307', 'SB0313', 'SB0347')){
#     bill_stages$action_in_comm <- bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
#   }
#   rm(reported)
# }
# 
# 
# ##########################
# #### Nevada
# ##########################
# 
# aic_t <- c('dates discussed in comm.+[0-9]', '^from committee: [a-z]+', '^from concurrent committee on.+: [a-z]+',
#            'from committee without rec', 'committee hearing')
# abc_t <- c('from committee', 'read second time', 'engrossment', 'engrossed', 'read third time', 'declared.+emergency measure',
#            'place on.+', 'taken from.+')
# pc_t <- c('read third time.+passed', 'to senate\\.$', 'to assembly\\.$', 'title approved', 'to enrollment')
# law_t <- c('approved by the governor', 'chapter [0-9]+', '^effective [a-z]+')
# 
# ## Checking Action in Committee for pre-2011
# if(t < 2011){
#   chamber_hearing <- ifelse(substring(b_id, 1, 1) == "A", bills[i,]$hcomm_hearing, bills[i,]$scomm_hearing)
#   if(bill_stages$action_in_comm == 0 & !is.na(chamber_hearing)){
#     bill_stages$action_in_comm <- 1
#   }
# }
# 
# 
# ##########################
# #### New York
# ##########################
# 
# aic_t <- c("^reported", '^1st report', "held for consideration", "died in committee", "committee consideration")
# # If lacks majority support, dies in comm --> implies failed vote http://www.nyc.gov/html/moiga/pages/state/process.shtml
# # Key point = not all bills have reported or died meaning some just see no action
# abc_t <- c("^reported", '^(1st|2nd) report', "third reading", "3rd reading cal", "amended on third") 
# # print number [0-9]+[a-z] - print number a/b/c means number changed to 123A/B/C after amended
# # BUT.. dropping print number from regex as leads to errors (sponsor can pull bill, amend, and reintroduce --> Print number change)
# pc_t <- c("passed assem", "repassed assem", "passed sen", "repassed sen", "^delivered to assembly", "^delivered to senate", "^delivered to gov")
# law_t <- c("^signed chap")
# 
# 
# 
# ##########################
# #### Ohio
# ##########################
# ## ignore_chamber_switch = TRUE
# 
# if(t <= 2014){
#   aic_t <- c('^committee report')
#   abc_t <- c('^committee report: [a-z]+ and reported', '^committee report: reported', '^floor action')
#   pc_t <- c('passed on 3rd consideration', 'conference committee')
#   law_t <- c('approved by governor', 'effective date')
# }else{
#   aic_t <- c('^reported')
#   abc_t <- c('^reported', 'read on the floor', 'motion to reconsinder', 'recommitted', 're-referred')
#   pc_t <- c('^passed', 'received from the house', 'received from the senate') # adopted is only resolutions = dropped
#   law_t <- c('^effective')
# }
# 
# 
# 
# ##########################
# #### Oklahoma
# ##########################
# 
# aic_t <- c('^cr', 'do pass', 'do not pass', 'committee substitute', '^reported', 'recommendation to the full comm', '; cr filed')
# # ** Second reading occurs before committee referral
# # ** Not coding do not pass as advancing beyond committee
# # -- per rules, in House: a successful do not pass vote/CR constitues final action on bill --> != ABC
# # -- in senate: do not pass not permitted recommendation; failed do pass (as amended) = final action --> != ABC
# abc_t <- c('do pass', 'general order', '^amended', '^advanced', 'engrossed', 'third reading', 'considered')
# pc_t  <- c('measure passed', 'emergency passed', 'measure and emergency passed', 'engrossed.+to (senate|house)')
# law_t <- c('approved by governor', "becomes law without governor")
# 
# ### Check if Passed Without/Over Governor Veto
# if(bill_stages$law == 0 & grepl('^filed with secretary of state', tolower(hist_sub[nrow(hist_sub),]$action))){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# ### Check if Passed Chamber
# if(bill_stages$passed_chamber == 0 & any(grepl('veto|enrolled|sent to governor|ccr|conference', tolower(hist_sub$action))) ){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }
# 
# ##########################
# #### Oregon
# ##########################
# 
# aic_t <- c('reported out', '^recommendation', '^(without|minority|majority) recommendation', 
#            'do pass', 'do not pass', 'be adopted', 'do adopt','work session held', 
#            '(work session|public hearing): heard', '(work session|public hearing) held')
# abc_t <- c('reported out', '^recommendation', '^(without|minority|majority) recommendation',
#            'second reading', 'third reading', '^pass', '^fail')
# pc_t  <- c('third reading.+passed', '^passed\\.')
# law_t <- c('governor signed', '^chapter [0-9]+', 'effective date', "filed with secretary of state without governor's")
# 
# #### Check Style/Form Vetoes -- If gov returns for style/form, law after majority of each house passes
# if(bill_stages$law == 0 & !is.na(bills[i,]$chapter_num) ){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# 
# ##########################
# #### Pennsylvania
# ##########################
# 
# aic_t <- c('^reported', 'reported as committed', 'reported as amended', "reported with request") # ^reported captures the 3 that follow..
# abc_t <- c("^reported", "removed from table", "^re-referred", "^re-committed", "second consideration", "third consideration", 
#            "defeated on final", "vote.+ reconsider", "placed on.+calendar")
# pc_t <- c("final passage", "signed in")
# law_t <- c('approved by the gov', 'act no\\. [0-9]')
# 
# if(bill_stages$law == 0 & grepl("^Act No\\. [0-9]+", bills[i,]$status)){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# 
# ##########################
# #### Rhode Island
# ##########################
# 
# ### Adjust Problematic Actions for Coding
# bill_hist$action <- gsub("House passed floor amendment only", 'Floor amendment passed by House', bill_hist$action)
# bill_hist$action <- gsub("Senate passed floor amendment only", 'Floor amendment passed by Senate', bill_hist$action)
# 
# aic_t <- c('committee recommend(ed|s)', 'scheduled for hearing', 'scheduled for consideration', 'committee heard', 'committee transferred')
# # "A bill goes on the calendar when a given committee makes a recommendation of passage" -- http://www.rilin.state.ri.us/pages/generalbilllaw.aspx
# # ----> If a 'Committee recommends no passage' the bill does not go anywhere
# abc_t <- c('committee recommends passage', 'placed on (house|senate) calendar', 'placed on (house|senate) consent calendar',
#            '^(house|senate) voted', '^(house|senate) passed', 'defeated by the (house|senate)',
#            'floor amendment', '^(house|senate) read and passed')
# pc_t  <- c('^(house|senate) passed', '^(house|senate) read and passed')
# law_t <- c('signed by governor', 'effective without governor', 'effective notwithstanding governor')
# 
# ### Checking if Bills Passed but were then reconsidered and never actually made it out of the chamber
# if(bill_stages$passed_chamber == 1 & bill_stages$law == 0){
#   if(any(grepl("voted to reconsider", tolower(hist_sub$action))) & length(unique(hist_sub$chamber)) == 1){
#     bill_stages$passed_chamber <- 0
#   }
# }
# 
# 
# ##########################
# #### South Carolina
# ##########################
# 
# aic_t <- c('committee report', 'tabled in committee', 'referred to subcommittee', 'polled out of committee', 'delegation report')
# # --> Per rules, polled out of committee is a vote by the committee to release it - https://scstatehouse.gov/senatepage/SRULES2019.pdf
# # --> Bills are sometimes referred to county delegations instead of committees -- E.G.: H3072 1989-1990
# abc_t <- c('committee report: fav', 'committee report: majority fav', 'committee report: recommend', 
#            'delegation report: fav', 'delegation report: majority fav', 'delegation report: recommend',
#            'read second time', 'second reading', 'read third time', 'third reading', '^amended', '^debate', '^objection', 'recommitted to')
# # Recommend =  Recommended refer to different committee
# pc_t <- c('read third time and sent to senate', 'read third time and sent to house', 'enrolled')
# law_t <- c('signed by governor', 'act no\\. [0-9]+', 'effective date [0-9]+')
# 
# 
# ##########################
# #### South Dakota
# ##########################
# 
# #### Adjusting Committee Report Actions
# bill_hist <- mutate(bill_hist, action = ifelse(grepl('do pass|do not pass|report without recommend|place on.+calendar', action) & !grepl("house|senate", action), paste0('committee: ', action), action))
# 
# 
# aic_t <- c('committee hearing', 'scheduled for hearing', '^(house|senate).+ amendment \\([a-z]-[0-9]+\\)',
#            'committee.+(do pass|do not pass|without recommend)', '^committee:.+calendar')
# # committee amendments distinguished by numbers in parentheticals at end of line
# abc_t <- c('committee.+do pass.+passed', 'motion to amend.+(h\\.j\\.|s\\.j\\.)', '^(house|senate).+on calendar', 'second reading',
#            '^house of representatives do pass', '^senate do pass', '^placed on consent',
#            'motion to strike the \"not\"')
# # Bills reported 'do not pass' or 'without recommendation' don't automatically get put on calendar; require motion on floor
# # --> See rule 6f-6: http://sdlegislature.gov/docs/legsession/2017/2017Redbook.pdf
# # --> Motion to strike the not = floor action to convert a do not pass report into a do pass report (following a vote)
# # Need to qualify motion to amend with hj/sj as committee motions to amend recorded in later years (and sometimes coded as house appropriations motion...)
# pc_t  <- c('^house of representatives do pass.+passed', '^senate do pass.+passed')
# law_t <- c('signed by governor', 'signed by the governor', 'delivered to sec. of state',
#            'delivered veto override to the secretary of state')
# 
# #### Check Style/Form Vetoes -- If gov returns for style/form, law after majority of each house passes
# if(bill_stages$law == 0 & any(grepl('^house.+vetoed for style.+passed', hist_sub$action)) & any(grepl('^senate.+vetoed for style.+passed', hist_sub$action))){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# #### Check Overrides
# if(bill_stages$law == 0 & any(grepl('^house.+veto override passed', hist_sub$action)) & any(grepl('^senate.+veto override passed', hist_sub$action))){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# #### Committee Motions to Amend
# if(bill_stages$action_in_comm == 0 & any(grepl("motion to amend", hist_sub$action)) & !any(grepl('motion to amend.+(h\\.j\\.|s\\.j\\.)', hist_sub$action))){
#   bill_stages$action_in_comm <- 1
# }
# #### Check Passages that are Reconsidered, Don't Leave Chamber
# if(bill_stages$passed_chamber == 1 & bill_stages$law == 0){
#   if(any(grepl("(house of representatives|senate) reconsidered, passed", hist_sub$action)) & length(unique(hist_sub$chamber)) == 1){
#     bill_stages$passed_chamber <- 0
#   }
# }
# 
# ##########################
# #### Tennessee
# ##########################
# ### ** ignore_chamber_switch = TRUE --- Need this to account for random miscoded chambers (e.g., HB0731 in 2005)
# 
# ### Get rid of periods + Excess Space in action text
# bill_hist$action <- gsub("\\.", '', bill_hist$action)
# bill_hist$action <- str_trim(gsub(" +", ' ', bill_hist$action))
# 
# ### Adjust Subcommmittee Recommendation Rows to distinctly seperate from comm reports
# bill_hist <- mutate(bill_hist, action = ifelse(grepl("rec.+for pass.+s/c", tolower(action)), paste0("subcomm rec: ", action), action))
# 
# aic_t <- c("placed on s/c cal", 's/c ref to', "rec for pass", "rec\\. for pass",  "recommended for pass", "^subcomm rec",
#            "placed on cal.+ comm", "placed on.+ comm cal", "failed in s/c", 'failed in.+committee') # -- S/C failures imply hearing/vote
# abc_t <- c("placed on.+(regular|consent|local bill) calendar", "ref.+calendar \\& rules comm", "ref.+s cal comm", "^(rec|rec\\.|recommended) for pass",
#            "^(h|house) adopted am", "^(s|senate) adopted am", "passed (h|s)", "^failed (h\\b|s\\b|house|senate)", "failed to pass (h|s)",
#            "amendment.+withdrawn", "engrossed", 'comp.+subst', 'subst.+for comp')
# ## -- Can't use "^rec\\. for pass\\. ref" or "recommended for pass" generally because subcomms use same language
# ## -----> Instead: Updating S/C Recs above so language is unique
# ## -- ALso: ABC if referred to H Calendar & Rules Committee/S Calendar Comm --> Means made it out of origin committee
# pc_t <- c("engrossed", "^passed h", "^passed s")
# law_t <- c("^pub ch|^pr ch|signed by governor")
# # *** This will not count companions that became law (which always are preceded by "Comp. became Pub Ch. N")
# 
# 
# ##########################
# #### Texas
# ##########################
# 
# ### Committee Status Variable for ABC Check
# bills <- bills %>% mutate(main_chamb_comm_status = ifelse(substring(bill_id,1,1) == "H", hc_status, sc_status))
# 
# 
# aic_t <- c('public hearing', 'testimony', 'pending in committee', '^reported f')
# # -- 'public hearing' captures scheduling and consideration / 'testiony' = recieved + taken in comm and subcomm
# # -- '^reported f' = favorably and unfavorably from comm and subcomm but NOT reported engrossed or enrolled
# abc_t <- c('^reported favorably w', '1st printing sent', 'calendar', 'read 2nd time', 'point of order', '^amend', 'motion to', 
#            'record vote', 'read 3rd time', 'rules suspended', 'pass', 'fail')
# # '^amend = amended, amends offered, amend tabled, amend withdrawn
# pc_t <- c('^passed$', '^passed as amended$', '^reported engross', 'sent to the senate', 'sent to the house')
# # -- Need to be careful with passed -- passed to engrossment = passed to vote on engrossment
# law_t <- c('signed by the gov', '^effective', 'filed w/o the gov.+ signature')
# 
# 
# ### Check if out of committee in originating chamber
# if(bill_stages$action_beyond_comm == 0 & !is.na(bills[i,]$main_chamb_comm_status) & tolower(bills[i,]$main_chamb_comm_status) == "out of committee"){
#   bill_stages$action_beyond_comm <- 1
# }
# ### Check if In or Out of Committee in Opposing Chamber
# if(bill_stages$passed_chamber == 0 & substring(b_id,1,1) == "H" & !is.na(bills[i,]$sc_status) & bills[i,]$sc_status != ""){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }else if(bill_stages$passed_chamber == 0 & substring(b_id,1,1) == "S"  & !is.na(bills[i,]$hc_status) & bills[i,]$hc_status != ""){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }
# 
# 
# ##########################
# #### Utah
# ##########################
# 
# ### Get Rid of Slashes in Actions
# bill_hist$action <- gsub('^House \\/|^House\\/', 'House ', bill_hist$action)
# bill_hist$action <- gsub('^Senate \\/|^Senate\\/', 'Senate ', bill_hist$action)
# bill_hist$action <- gsub('  +', ' ', bill_hist$action)
# 
# # *** Note: Format changes in 2002, regex vectors capture both pre and post****
# aic_t <- c('^(house|senate) comm.+(report|rpt)', '^(house|senate) comm.+motion',
#            '^(house|senate) comm.+(recommend|favorable|on consent)',
#            '^(house|senate) comm.+amendment recommendation')
# abc_t <- c('^(house|senate) comm.+(report|rpt)', 'read 2nd', 'read 3rd', 'placed on (2nd|3rd)', 
#            'read 2nd \\& 3rd', '2nd reading', '3rd reading', 'pass (2nd|3rd)', 'passed (2nd|3rd)',
#            'placed on.+calendar', 'floor amendment', '^(house|senate) amended',
#            '^(house|senate) substitute')
# ## Enacting Clause Struck == Dead, Motion to Reconsider out of order thereafter (Occurs via motion or at end of session)
# pc_t  <- c('^(house|senate) passed 3rd', '^(house|senate) pass 3rd', '^(house|senate) pass 2nd \\& 3rd',
#            '^house.+to senate', '^senate.+to house') 
# law_t <- c('governor signed', 'became law w.+ governor signature', 'governor line item veto')
# 
# ##### Check if Passed Without/Over Governor Veto
# if(bill_stages$law == 0 & (!is.na(bills[i,]$chapter_num) | grepl('Governor Signed|Became Law|Governor Line Item Veto', bills[i,]$last_action)) ){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# #### Check if Passed Chamber
# if(bill_stages$passed_chamber == 0 & any(grepl('to Governor|to Lieutenant Gov|received from.+(House|Senate)', hist_sub$action)) ){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }
# 
# ##########################
# #### Virginia
# ##########################
# 
# #### Adusting Defeated By Records
# bill_hist <- bill_hist %>%
#   mutate(action = ifelse(grepl("defeated by ", action) & !grepl("defeated by (house|senate)", action), 
#                          gsub("defeated by ", "defeated in committee: ", tolower(action)), action))
# 
# 
# aic_t <- c("assigned to .+ sub-comm", "assigned.+ sub:", "reported from", "subcommittee recomm", 
#            "subcommittee failed to", "tabled in", "failed to report", "^continued to \\d{4} in", 
#            "^passed by .+ in", 'defeated in committee:', 'stricken from docket by', 'letter sent to')
# ### -- Tabled in + Passed by Indefinitely + Stricken + Continuation/Carry Over Requires a Vote
# ### -- Committees have authority to send letters to relevant agencys/study commissions (not required, but happens often)
# abc_t <- c("^reported from", "read second time", "read third time", "engrossed", "vote:", "passed house", 
#            "passed senate", "defeated by house", "defeated by senate")
# pc_t <- c("passed house", "passed senate", "agreed to by house", "agreed to by senate", "vote: passage", 
#           "vote: block vote passage", "vote: adoption", "vote: block vote adoption", "signed by speaker", 'signed by president')
# # Block Vote = 100-0
# law_t <- c("^approved by gov|^acts of assembly chapter text ")
# 
# ### Double CHeck Passage to Account for things like this: 
# if(bill_stages$passed_chamber == 1 & bill_stages$law == 0){
#   if(!( any(grepl("communicated to (house|senate)", hist_sub$action)) | length(unique(hist_sub$chamber)) > 1)){
#     bill_stages$passed_chamber <- 0
#   }
# }
# 
# ##########################
# #### Vermont
# ##########################
# 
# # *** 'RELVD Report Relieved by [Comm]' == Motion to relieve committee of consideration of a bill (from floor)
# # ----> Sometimes rereffered, sometimes discharged, so not coding as ABC... if discharged, presumably 2nd reading will show up
# # *** UNFAV reports appear to go to floor just like favorable reports
# if(t < 2009){
#   aic_t <- c('reported from', 'f/w/a report', 'favor report', 'unfav report', 'n/rec report')
#   abc_t <- c('reported from',  'f/w/a report', 'favor report', 'unfav report', 'n/rec report',
#              '(second|2nd) reading', '(third|3rd) reading', '^amend on', 'floor amendment', 'roll call vote')
#   pc_t  <- c('^passed on (third|3rd)') 
#   law_t <- c('signed by governor')
# }else{
#   ### Committee actions are sparse post-2008
#   aic_t <- c('(reported|recommended) for the committee', "report of committee", 'reported (favor|unfav|without rec)', 
#              '(favor[a-z]+|unfav[a-z]+) report', 'spoke for the comm', 'amendment of comm')
#   abc_t <- c('(reported|recommended) for the committee', 'report of committee', 'reported (favor|unfav|without rec)',
#              'read (second|2nd) time', 'read (third|3rd) time', '(second|2nd) reading', '(third|3rd) reading',
#              '^proposal of amend', '^(house|senate) proposal of amend', 'roll call')
#   pc_t  <- c('read (third|3rd) time and passed', 'read (third|3rd) time \\& passed') 
#   law_t <- c('signed by governor', 'allowed.+without.+signature.+governor', 'enacted without governor')
# }
# 
# #### Check IF Act Number Present
# if(bill_stages$law == 0 & !is.na(bills[i,]$act_num) ){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
# ### Check if Made it to Opposing Chamber
# if(bill_stages$passed_chamber == 0 & any(grepl('House', hist_sub$chamber)) & any(grepl('Senate', hist_sub$chamber))){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
# }
# #### Checks for 2009+
# if(t >= 2009){
#   if(bill_stages$law == 0 & any(grepl("allowed to go into effect|signed by gov", tolower(hist_sub$status)))){
#     bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1        
#   }
#   if(bill_stages$passed_chamber == 0 & any(grepl('^passed', tolower(hist_sub$status)))){
#     bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- 1
#   }
# }
# 
# ##########################
# #### Washington
# ##########################
# # *** ignore_chamber_switch = TRUE
# 
# ## Adding "committee_" as prefix to committee actions to make AIC easier to code
# bill_hist$action <- ifelse(grepl('^[A-Z][A-Z]+ - ', bill_hist$action), paste0('Committee_', bill_hist$action), bill_hist$action)
# 
# ## **** Note: Actions become a bit more detailed from 2003 on -- specifically committee actions ***
# aic_t <- c('committee_', '^minority', 'do pass', 'do not pass', 'without recommendation', 'do confirm', 'committee amendment',
#            'public hearing', 'executive session', 'executive action taken')
# ## --> "Committee_ZZZ - Majority" == Committee Report (signed by majority); if a minority report, included on next line
# abc_t <- c('committee_.+majority', '^passed to rules committee', 'referred to rules 2 review',
#            'second reading', 'third reading', 
#            'committee amend.+adopted', '^amended\\.')
# ## --> If a majority report OR if to rules = Reported out; need both because first will also catch bills that then get referred to a new comm
# pc_t <- c('third reading, passed')
# law_t <- c('governor signed', 'governor partially vetoed', '^chapter [0-9]+', '199[0-9] laws', '20[0-9][0-9] laws')
# 
# ##########################
# #### Wisconsin
# ##########################
# 
# ### Adjusting Actions that Create Issues
# bill_hist$action <- ifelse(grepl("joint survey committee", tolower(bill_hist$action)), paste0("~Survey Committee~ ", bill_hist$action), bill_hist$action)
# 
# ##### Set State-Specific Terms for Identifying Each Stage
# # -- Executive action taken = committee executive action/debate -- See here, page 32: http://legis.wisconsin.gov/lrb/media/1093/14rb2.pdf
# # -- Amendments: Currently only catching committee amendments; could add 'amend.+offered by rep|amend.+offered by sen'
# # -----> Problem is that sometimes these are in committee stage, sometimes floor stage, and parsing will require an overhaul of the bill_hist fx script + not clear it matters
# aic_t <- c('public hearing held', 'recommended by.+comm', 'amend.+offered.+committee', 'estimate received', 'executive action taken', 
#            "placed on calendar [^\\s]+ by committee")
# abc_t <- c('^report', 'placed on cal.+ by rules', 'read a second time', 'read a third time', 'second reading', 
#            'third reading', '^failed to pass.+gov', '^referred to cal', 'rules suspended') 
# # Note: '^referred to cal' needs ^ or else will catch first read
# # ---- Failed to pass pursuant to Assembly/Senate Joint Res ZZ appears to just clear bills out at end of session --- Not coding as action
# pc_t <- c('^read.+ passed', '^passed', 'enrolled')
# law_t <- c('approved by the gov', '^published', 'wisconsin act [0-9]+')
# 
# 
# ##########################
# #### West Virginia
# ##########################
# 
# aic_t <- c('^reported (do|be|with|in)', 'do pass', '^reported be adopted', 'committee.+reported', 'committee amendment', '^originating in')
# # -- Bills can originate in committee in WV: Implies written in comm (AIC); also does not need to then be referred 
# abc_t <- c('^reported', '(1st|2nd|3rd|first|second|third) reading', 'read (1st|2nd|3rd)',
#            'house calendar', 'special calendar','^effective', 'roll no\\. [0-9]+', 'voice vote',
#            'suspension of', 'unanimous consent')
# # -- "^Effective" = Effective [date]/Effective from passage = vote on effective date
# pc_t <- c('^passed', 'communicated to (house|senate)', 'ordered to (house|senate)',
#           'to governor', 'completed legislative action')
# law_t <- c('approved by governor', 'chapter [0-9]+', 'acts 19[0-9]+', 'acts 20[0-9]+')
# # -- "acts [year]" only used in earlier sessions. In 2018 its: "Acts, [Session Type], [Year]
# 
# ### Check if Not Actually Law - e.g. http://www.wvlegislature.gov/Bill_Status/Bills_history.cfm?input=4040&year=2016&sessiontype=RS&btype=bill
# if(bill_stages$law == 1 & any(grepl("clerk\\'s note.+bill is null and void", tolower(hist_sub$action))) ){
#   bill_stages$law <- 0
# }
# 
# ##########################
# #### Wyoming
# ##########################
# ## *** Note: Formats switch a bit from 2015 onward ***
# 
# ##### Set State-Specific Terms for Identifying Each Stage
# aic_t <- c('^h[0-9]+ recommend', '^s[0-9]+ recommend', '^h[0-9]+ - [a-z]+:recommend', '^s[0-9]+ - [a-z]+:recommend',
#            'do pass', 'do not pass', '(without|no) recommendation')
# abc_t <- c('^h[0-9]+ recommended.+do pass', '^s[0-9]+ recommended.+do pass', 'placed on general file', 'rerefer', 
#            '^amendment', '^(h|s) amendments', "(passed|failed|considered in) cow", "cow:(pass|fail|consider)",
#            '2nd reading', '3rd reading')
# # "As a matter of practice, "DO NOT PASS" or "NO RECOMMENDATION" bills are generally not immediately reported out of Committee." -- See process handbook
# # "Committee Returned Bill Pursuant to HR 4-3(b/c/etc)" != ABC --> Just means that consideration period ended and committee forwarded records to clerk, but clerk not required to proceed with reading for chamber
# pc_t <- c('^(h|s) passed 3rd', '3rd reading:passed')
# law_t <- c('governor signed', 'assigned chapter number')
# 
# 
# ### Check if Chapter Number Recorded Despite No Regex Match
# if(bill_stages$law == 0 & bills[i,]$chapter_num != '' & bills[i,]$chapter_num != 'CH0000'){
#   bill_stages$action_beyond_comm <- bill_stages$passed_chamber <- bill_stages$law <- 1
# }
